/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pope.jloading.downloader;

/**
 *
 * @author pope945@gmail.com
 */
public class Piece {
    private long start;  // 起始点的指针位置
    private long pos;    // 当前完成度所在的指针位置
    private long end;    // 终结点的指针位置
    
    public Piece(long start, long pos, long end) {
        this.start = start;
        this.pos = pos;
        this.end = end;
    }

    public synchronized long getEnd() {
        return end;
    }

    public synchronized void setEnd(long end) {
        this.end = end;
    }

    public synchronized long getPos() {
        return pos;
    }

    public synchronized void setPos(long pos) {
        this.pos = pos;
    }

    public synchronized long getStart() {
        return start;
    }

    public synchronized void setStart(long start) {
        this.start = start;
    }
    
    /**
     * 切割出一块新的区�?     * @return
     */
    public synchronized Piece cutPiece() {
        // 如果当前区块还有大于xK的数据未下载完，则允许切割出分块
        long toCut = end - pos;
        if (toCut > (1024 * 10)) {
            long s; // 起点
            long e; // 终点
            s = (long) (pos + Math.ceil(toCut / 2));
            e = end;
            end = s - 1;
            Piece piece = new Piece(s, s, e);
            return piece;
        }
        return null;
    }
}
